package com.github.xiaolyuh.aspect;

import com.github.xiaolyuh.annotation.FlushType;
import com.github.xiaolyuh.utils.FlushHolder;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;


@Aspect
@Component
public class FlushAspect {

    @Around("@annotation(com.globalegrow.promotion.cache.manager.annotation.FlushType) && " +
            "@annotation(annotation)")
    public Object execute(ProceedingJoinPoint joinPoint, FlushType annotation) throws Throwable {
        try {
            FlushHolder.set(annotation.value());
            return joinPoint.proceed();
        } catch (Throwable throwable) {
            throw throwable;
        } finally {
            FlushHolder.clear();
        }
    }

}